﻿using System;
using System.Collections.Generic;
using System.Linq;
using DispatchAssist.BusinessLogical.DAL;
using DispatchAssist.BusinessLogical.Model;


namespace DispatchAssist.BusinessLogical.BLL
{
    public class FTPFileManager
    {
        private IDataProvider provider;
        static log4net.ILog Logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        static object locker = new object();

        public FTPFileManager()
        {
            this.provider = new SqlDataProvider();
        }

        public FTPFileManager(IDataProvider p)
        {
            this.provider = p;
        }


        public Dictionary<string, DateTime> GetRecentFileNames(int recentDays)
        {
            Dictionary<string, DateTime> result = null;

            try
            {
                lock (locker)
                {
                    result = provider.GetRecentFileNames(recentDays);
                }
            }
            catch (Exception e)
            {
                Logger.Error("Error in GetRecentFileNames", e);
            }

            return result;
        }



        public int DetectCreateMeteoMsgs(List<MeteoMsg> files)
        {
            int count = 0;
            foreach (var item in files)
            {
                try
                {
                    provider.CreateMeteoMsg(item);
                    count++;
                }
                catch (Exception e)
                {
                    Logger.Error("Error During CreateMeteoMsg", e);
                    continue;
                }
            }

            return count;
        }


    }
}
